﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using NPMonitor.DataModel;
using NPMonitor.Implementation;

namespace NPMonitor.Web.UserControls
{
    public partial class SettingList : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                UpdateNotification();
                RenderNotification();
            }
            else
            {
                RenderNotification();
            }
        }

        /// <summary>
        /// 保存操作
        /// </summary>
        private void UpdateNotification()
        {
            StoreManager.Current.NotificationStore.UpdateNotificationsByUserId(CurrentUserId, Request.Form);
            phSaveSuccessful.Visible = true;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void RenderNotification()
        {
            IEnumerable<JobDefinition> jobs = StoreManager.Current.JobDefinitionStore.GetAllJobDefinitions(0, int.MaxValue);
            IEnumerable<NotificationData> notifications = StoreManager.Current.NotificationStore.GetAllNotificationsByUserId(CurrentUserId);

            BindTableContent(tbNotifications, jobs, notifications);
        }

        /// <summary>
        /// 绑定表格table里的数据
        /// </summary>
        /// <param name="talbe"></param>
        /// <param name="jobs"></param>
        /// <param name="notifications"></param>
        private void BindTableContent(HtmlTable talbe, IEnumerable<JobDefinition> jobs, IEnumerable<NotificationData> notifications)
        {
            foreach (JobDefinition jobInfo in jobs)
            {
                HtmlTableRow row = new HtmlTableRow();

                row.Cells.Add(new HtmlTableCell
                {
                    InnerText = jobInfo.JobName
                });
                //row.Cells[0].Attributes["title"] = jobInfo.JobId.ToString();

                foreach (int myCode in Enum.GetValues(typeof(NotificationEvent)))
                {
                    string strName = Enum.GetName(typeof(NotificationEvent), myCode);//获取名称
                    string strVaule = myCode.ToString();//获取值

                    bool isChecked = notifications.FirstOrDefault(p => p.Event.Trim() == strName.Trim() && p.TaskId == jobInfo.JobId && p.UserId == CurrentUserId) != null ? true : false;

                    row.Cells.Add(new HtmlTableCell
                    {
                        InnerHtml = String.Format("<input name='{0}${1}' type='checkbox' {2} />",
                            jobInfo.JobId,
                            strName,
                            (isChecked ? "checked" : String.Empty))
                    });
                }
                tbNotifications.Rows.Add(row);
            }
        }

    }
}